/*******************************************************************************
 * Package: com.hngtrust.iir.company.service.impl
 * Type:    DuPontAnalysisServiceImpl
 * Date:    2021/4/25 19:34
 *
 * Copyright (c) 2021 HUANENG GUICHENG TRUST CORP.,LTD All Rights Reserved.
 *
 * You may not use this file except in compliance with the License.
 *******************************************************************************/
package com.hngtrust.iir.company.service.impl;

import com.hngtrust.iir.company.controller.vo.CompFinanceTrendReqVO;
import com.hngtrust.iir.company.controller.vo.ListCompDpAnalyDataRespVO;
import com.hngtrust.iir.company.controller.vo.ListCompDpAnalysRespVO;
import com.hngtrust.iir.company.controller.vo.ListCompDpAnalysTrendRespVO;
import com.hngtrust.iir.company.controller.vo.ListFiancialReqVo;
import com.hngtrust.iir.company.entity.CompDpAnalys;
import com.hngtrust.iir.company.entity.CompDpEvaluate;
import com.hngtrust.iir.company.service.CompDpAnalysService;
import com.hngtrust.iir.company.service.CompDpEvaluateService;
import com.hngtrust.iir.company.service.DuPontAnalysisService;
import com.hngtrust.iir.constant.Constant;
import com.hngtrust.iir.exception.IirErrorType;
import com.hngtrust.iir.exception.IirException;
import com.hngtrust.iir.util.ListUtil;
import org.springframework.beans.BeanUtils;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;

import java.util.ArrayList;
import java.util.Calendar;
import java.util.List;

/**
 * 杜邦分析接口实现类
 *
 * @author Wanghan
 * @date 2021/4/25 19:34
 */

@Service
public class DuPontAnalysisServiceImpl implements DuPontAnalysisService {
    
    @Autowired
    private CompDpAnalysService compDpAnalysService;
    
    @Autowired
    private CompDpEvaluateService compDpEvaluateService;
    
    /**
     *杜邦分析
     */
    @Override
    public List<ListCompDpAnalysRespVO> getCompDpAnalysRespVO(ListFiancialReqVo req) {
    
        if(!(req.getCompId() > 0L)){
            throw new IirException(IirErrorType.INTFACE_PARAMS_ERROR,"ID");
        }
        List<ListCompDpAnalysRespVO> resp = new ArrayList<>();
        List<ListCompDpAnalyDataRespVO> listCompDpAnalyDataRespVO = new ArrayList<>();
        ListCompDpAnalysRespVO listCompDpAnalysRespVO = new ListCompDpAnalysRespVO();
        CompDpAnalys compDpAnalys = new CompDpAnalys();
        compDpAnalys.setCompId(req.getCompId());
        compDpAnalys.setStatus(Constant.DEFAULT_STATUS_NORMAL);
        List<Integer> years = getFinanceGradeByYear();
        List<CompDpAnalys> list = new ArrayList<>();
        for(int year:years){
            compDpAnalys.setYear(year);
            List<CompDpAnalys> compDpAnalysList = compDpAnalysService.queryList(compDpAnalys);
            //杜邦分析数据
            if(compDpAnalysList != null && !compDpAnalysList.isEmpty()){
                list.add(compDpAnalysList.get(0));
                ListCompDpAnalyDataRespVO cListCompDpAnalyDataRespVO = new ListCompDpAnalyDataRespVO();
                cListCompDpAnalyDataRespVO = getCompDpAnalyDataRespVO(compDpAnalysList.get(0));
                listCompDpAnalyDataRespVO.add(cListCompDpAnalyDataRespVO);
            }else{
                CompDpAnalys dpAnalys = new CompDpAnalys();
                dpAnalys.setNetAssetRate(null);
                dpAnalys.setTotalAssetRate(null);
                dpAnalys.setNetMargin(null);
                dpAnalys.setEm(null);
                list.add(dpAnalys);
                ListCompDpAnalyDataRespVO cListCompDpAnalyDataRespVO = new ListCompDpAnalyDataRespVO();
                cListCompDpAnalyDataRespVO.setCompId(req.getCompId());
                cListCompDpAnalyDataRespVO.setYear(year);
                listCompDpAnalyDataRespVO.add(cListCompDpAnalyDataRespVO);
            }
        }
        listCompDpAnalysRespVO.setListCompDpAnalyDataRespVO(listCompDpAnalyDataRespVO);
    
    
    
        //净资产收益率（ROE） 判断上升还是下降趋势
        int netAssetRateTrend =  getNetAssetRateTrend(list);
        listCompDpAnalysRespVO.setNetAssetRateTrend(netAssetRateTrend);
        //总资产周转率
        int totalAssetRateTrend = getTotalAssetRateTrend(list);
        listCompDpAnalysRespVO.setTotalAssetRateTrend(totalAssetRateTrend);
        //净利率
        int netMarginTrend = getNetMarginTrend(list);
        listCompDpAnalysRespVO.setNetMarginTrend(netMarginTrend);
        //权益乘数
        int emTrend = getEmTrend(list);
        listCompDpAnalysRespVO.setEmTrend(emTrend);
        
        resp.add(listCompDpAnalysRespVO);
        return resp;
    }
    
    /**
     * 杜邦分析走势图
     */
    @Override
    public List<ListCompDpAnalysTrendRespVO> getCompDpAnalysTrendRespVO(CompFinanceTrendReqVO req) {
    
        if(!(req.getCompId() > 0L)){
            throw new IirException(IirErrorType.INTFACE_PARAMS_ERROR,"ID");
        }
        CompDpAnalys compDpAnalys = new CompDpAnalys();
        List<ListCompDpAnalysTrendRespVO> resp = new ArrayList<>();
        List<CompDpAnalys> lists = new ArrayList<>();
        compDpAnalys.setCompId(req.getCompId());
        compDpAnalys.setStatus(Constant.DEFAULT_STATUS_NORMAL);
        List<Integer> years = getFinanceGradeByYear5();
        for(int year:years){
            compDpAnalys.setYear(year);
            List<CompDpAnalys> compDpAnalysList = compDpAnalysService.queryList(compDpAnalys);
            if(compDpAnalysList != null && !compDpAnalysList.isEmpty()){
                lists.add(compDpAnalysList.get(0));
            }else{
                CompDpAnalys dpAnalys = new CompDpAnalys();
                dpAnalys.setCompId(req.getCompId());
                dpAnalys.setYear(year);
                lists.add(dpAnalys);
            }
            
        }
        ListUtil<ListCompDpAnalysTrendRespVO> listUtil = BeanUtils.instantiateClass(ListUtil.class);
        listUtil.copyList(lists,resp,ListCompDpAnalysTrendRespVO.class);
        return resp;
    }
    
    /**
     * 杜邦分析数据
     */
    public ListCompDpAnalyDataRespVO getCompDpAnalyDataRespVO(CompDpAnalys dpAnalys){
        ListCompDpAnalyDataRespVO listCompDpAnalyDataRespVO = new ListCompDpAnalyDataRespVO();
        List<CompDpEvaluate> compDpEvaluateList = new ArrayList<>();
        listCompDpAnalyDataRespVO.setId(dpAnalys.getId());//Id
        listCompDpAnalyDataRespVO.setCompId(dpAnalys.getCompId());//公司Id
        listCompDpAnalyDataRespVO.setYear(dpAnalys.getYear());//年份
        listCompDpAnalyDataRespVO.setNetAssetRate(dpAnalys.getNetAssetRate());//净资产收益率（ROE）
        listCompDpAnalyDataRespVO.setTotalAssetRate(dpAnalys.getTotalAssetRate());//总资产周转率
        listCompDpAnalyDataRespVO.setNetMargin(dpAnalys.getNetMargin());//净利率
        listCompDpAnalyDataRespVO.setEm(dpAnalys.getEm());//权益乘数
        listCompDpAnalyDataRespVO.setLoar(dpAnalys.getLoar());//资产负债比率
        listCompDpAnalyDataRespVO.setCurrentRate(dpAnalys.getCurrentRate());//流动比率
        listCompDpAnalyDataRespVO.setQuickRate(dpAnalys.getQuickRate());//速动比率
        listCompDpAnalyDataRespVO.setInterestCoverage(dpAnalys.getInterestCoverage());//利息保障倍数
        listCompDpAnalyDataRespVO.setAssetRate(dpAnalys.getAssetRate());//流动资产比率
        listCompDpAnalyDataRespVO.setNonAssetRate(dpAnalys.getNonAssetRate());//非流动资产比率
        listCompDpAnalyDataRespVO.setReceivableRate(dpAnalys.getReceivableRate());//应收账款周转率
        listCompDpAnalyDataRespVO.setInventoryRate(dpAnalys.getInventoryRate());//存货周转率
        listCompDpAnalyDataRespVO.setRateMargin(dpAnalys.getRateMargin());//毛利率
        listCompDpAnalyDataRespVO.setProfitRate(dpAnalys.getProfitRate());//营业利润率
        //杜邦分析话术
        CompDpEvaluate compDpEvaluate = new CompDpEvaluate();
        compDpEvaluate.setDpAnalysId(dpAnalys.getId());
        compDpEvaluateList = compDpEvaluateService.queryList(compDpEvaluate);
        if(compDpEvaluateList !=null && !compDpEvaluateList.isEmpty()){
            listCompDpAnalyDataRespVO.setDriverConclusion(compDpEvaluateList.get(0).getDriverConclusion());
        }else{
            listCompDpAnalyDataRespVO.setDriverConclusion("");
        }
        
        return listCompDpAnalyDataRespVO;
    }
    
    /**
     *  净资产收益率（ROE） 判断上升还是下降趋势
     *  TREND_RISE : 上升  TREND_DECLINE ：下降  TREND_EQUAL:相等
     */
    public int getNetAssetRateTrend(List<CompDpAnalys> compDpAnalysList){
        int trend = 0;
        //今5年的趋势对比
        if(compDpAnalysList != null && !compDpAnalysList.isEmpty()){
            if(compDpAnalysList.get(0).getNetAssetRate() != null && compDpAnalysList.get(1).getNetAssetRate() != null && compDpAnalysList.size() >=2){
                if(compDpAnalysList.get(0).getNetAssetRate().doubleValue()>compDpAnalysList.get(1).getNetAssetRate().doubleValue()){
                    trend=Constant.TREND_RISE;
                }else if(compDpAnalysList.get(0).getNetAssetRate().doubleValue()<compDpAnalysList.get(1).getNetAssetRate().doubleValue()){
                    trend=Constant.TREND_DECLINE;
                }else{
                    trend=Constant.TREND_EQUAL;
                }
            }else{
                if(null == compDpAnalysList.get(0).getNetAssetRate()
                        && null != compDpAnalysList.get(1).getNetAssetRate()){
                    trend=Constant.TREND_DECLINE;
                }else if(null != compDpAnalysList.get(0).getNetAssetRate()
                        && null == compDpAnalysList.get(1).getNetAssetRate()){
                    trend=Constant.TREND_RISE;
                }else{
                    trend=Constant.TREND_EQUAL;
                }
            }
        }
        return trend;
    }
    
    /**
     *  总资产周转率 判断上升还是下降趋势
     *  TREND_RISE : 上升  TREND_DECLINE ：下降  TREND_EQUAL:相等
     */
    public int getTotalAssetRateTrend(List<CompDpAnalys> compDpAnalysList){
        int trend = 0;
        //今5年的趋势对比
        if(compDpAnalysList != null && !compDpAnalysList.isEmpty()){
            if(compDpAnalysList.get(0).getTotalAssetRate() != null && compDpAnalysList.get(1).getTotalAssetRate() != null && compDpAnalysList.size() >=2) {
                if (compDpAnalysList.get(0).getTotalAssetRate().doubleValue() > compDpAnalysList.get(1).getTotalAssetRate().doubleValue()) {
                    trend = Constant.TREND_RISE;
                } else if (compDpAnalysList.get(0).getTotalAssetRate().doubleValue() < compDpAnalysList.get(1).getTotalAssetRate().doubleValue()) {
                    trend = Constant.TREND_DECLINE;
                } else {
                    trend = Constant.TREND_EQUAL;
                }
            }else{
                if (null == compDpAnalysList.get(0).getTotalAssetRate()
                        && null != compDpAnalysList.get(1).getTotalAssetRate()) {
                    trend = Constant.TREND_DECLINE;
                } else if (null != compDpAnalysList.get(0).getTotalAssetRate()
                        && null == compDpAnalysList.get(1).getTotalAssetRate()) {
                    trend = Constant.TREND_RISE;
                } else {
                    trend = Constant.TREND_EQUAL;
                }
            }
        }
        return trend;
    }
    
    /**
     *  净利率 判断上升还是下降趋势
     *  TREND_RISE : 上升  TREND_DECLINE ：下降  TREND_EQUAL:相等
     */
    public int getNetMarginTrend(List<CompDpAnalys> compDpAnalysList){
        int trend = 0;
        //今5年的趋势对比
        if(compDpAnalysList != null && !compDpAnalysList.isEmpty()){
            if(compDpAnalysList.get(0).getNetMargin() != null && compDpAnalysList.get(1).getNetMargin() != null && compDpAnalysList.size() >=2){
                if(compDpAnalysList.get(0).getNetMargin().doubleValue()>compDpAnalysList.get(1).getNetMargin().doubleValue()){
                    trend=Constant.TREND_RISE;
                }else if(compDpAnalysList.get(0).getNetMargin().doubleValue()<compDpAnalysList.get(1).getNetMargin().doubleValue()){
                    trend=Constant.TREND_DECLINE;
                }else{
                    trend=Constant.TREND_EQUAL;
                }
            }else{
                if(null == compDpAnalysList.get(0).getNetMargin()
                        && null != compDpAnalysList.get(1).getNetMargin()){
                    trend=Constant.TREND_DECLINE;
                }else if(null != compDpAnalysList.get(0).getNetMargin()
                        && null == compDpAnalysList.get(1).getNetMargin()){
                    trend=Constant.TREND_RISE;
                }else{
                    trend=Constant.TREND_EQUAL;
                }
            }
        }
        return trend;
    }
    
    /**
     *  权益乘数 判断上升还是下降趋势
     *  TREND_RISE : 上升  TREND_DECLINE ：下降  TREND_EQUAL:相等
     */
    public int getEmTrend(List<CompDpAnalys> compDpAnalysList){
        int trend = 0;
        //今5年的趋势对比
        if(compDpAnalysList != null && !compDpAnalysList.isEmpty() && compDpAnalysList.size() >=2){
            if(compDpAnalysList.get(0).getEm() != null && compDpAnalysList.get(1).getEm() != null){
                if(compDpAnalysList.get(0).getEm().doubleValue()>compDpAnalysList.get(1).getEm().doubleValue()){
                    trend=Constant.TREND_RISE;
                }else if(compDpAnalysList.get(0).getEm().doubleValue()<compDpAnalysList.get(1).getEm().doubleValue()){
                    trend=Constant.TREND_DECLINE;
                }else{
                    trend=Constant.TREND_EQUAL;
                }
            }else{
                if(null == compDpAnalysList.get(0).getEm()
                        && null != compDpAnalysList.get(1).getEm()){
                    trend=Constant.TREND_DECLINE;
                }else if(null != compDpAnalysList.get(0).getEm()
                        && null == compDpAnalysList.get(1).getEm()){
                    trend=Constant.TREND_RISE;
                }else{
                    trend=Constant.TREND_EQUAL;
                }
            }
        }
        return trend;
    }
    
    
    /**
     *获取5近年的年份
     */
    public List<Integer> getFinanceGradeByYear(){
        Calendar cal = Calendar.getInstance();
        List<Integer> years = new ArrayList<>();
        int year = cal.get(Calendar.YEAR);
        for(int i = 0; i < 5; i++){
            year= year-1;
            years.add(year);
        }
        return years;
    }
    
    
    /**
     *获取5近年的年份 从小到大排序
     */
    public List<Integer> getFinanceGradeByYear5(){
        Calendar cal = Calendar.getInstance();
        List<Integer> years = new ArrayList<>();
        List<Integer> y = new ArrayList<>();
        int year = cal.get(Calendar.YEAR);
        for(int i = 0; i < 5; i++){
            year= year-1;
            years.add(year);
        }
        for(int i=years.size()-1;i>=0;i--){
            y.add(years.get(i));
        }
        return y;
    }
    
}
